<template>
	<div id="trunk">
		<lead v-show="showHead" @checkLogin="checkLogin" :pathOption="pathOption" :userData="userData"/>
		<main>
            <transition name="fade-transform" mode="out-in">
                <!-- :key="Date.now()" -->
                <router-view :pathOption="pathOption" :userData="userData"/>
            </transition>
        </main>
		<toTop />
		<foote />
	</div>
</template>

<script>
// 头部
import lead from './head';
// 底部
import foote from './foot';
// 回到顶部按钮
import toTop from './toTop';
export default {
	components: {
		lead,
		foote,
		toTop
	},
	data() {
		return {
			pathOption: {
				personal: '', //个人中心跳转路径
				manage: '' //后台跳转路径
            },
            userData:{
                userInfo: {}, //用户身份信息
                checkRole: null, //当前的身份
                role: [], //身份id数组
                managerole: false, //后台权限
			    personalFlag: false, //个人中心权限
                messageFlag: false, //我的消息
                isVistor: 0, //是否是游客
            }
		};
	},
	created() {},
	mounted() {
			console.log("Main")
        this.checkLogin();
    },
    computed: {
        showHead(){
            return ['cdkey','liveRoom'].includes(this.$route.name)?false:true;
        }
    },
	methods: {
        //查询登录情况
		checkLogin() {
            const protocol = sessionStorage.getItem("protocol");
            const host = sessionStorage.getItem('host');
			let success = data => {
                this.userData.userInfo = data.data.userinfo; //用户信息
                this.$g.user_id = data.data.userinfo.id;
				this.userData.checkRole = data.data.userinfo.check_role; //当前登录状态是什么身份
				this.userData.role = data.data.userinfo.role; //身份id数组
                // this.userData.login_out = data.data.logout_url;
                sessionStorage.setItem('user_info_str', JSON.stringify(data.data.userinfo));
                sessionStorage.setItem('token', data.data.token);
                localStorage.setItem('logintoken', data.data.token)
             
                let encodeToken = data.data.token;
                this.pathOption.personal = '/personal/login?token=' + encodeToken + '&protocol='+ protocol +'&host=' + host;
                this.pathOption.manage = '/admin/login?token=' + encodeToken + '&protocol='+ protocol +'&host=' + host;
				//后台权限
                this.userData.managerole = (typeof data.data.manage != 'undefined' && data.data.manage != null && data.data.manage != '')?true:false;
                // 个人中心权限
                this.userData.personalFlag = (typeof data.data.personal != 'undefined' && data.data.personal != null && data.data.personal != '')?true:false;
                // 我的消息显示
				if (typeof data.data.personal != 'undefined' && data.data.personal != null && data.data.personal != '') {
					var has_message = 0;
					for (let i = 0; i < data.data.node_personal.length; i++) {
						if (data.data.node_personal[i].name == 'message') {
							has_message = 1;
						}
					}
                    this.userData.messageFlag = has_message == 1?true:false;
				}
				//是否是游客
                this.userData.isVistor = data.data.userinfo.is_youke;
			};
			let fail = data => {
                this.$g.user_id=0;
                this.pathOption.personal = '/personal' + '?protocol='+ protocol +'&host=' + host;
                this.pathOption.manage = '/admin' + '?protocol='+ protocol +'&host=' + host;
                sessionStorage.removeItem("token");
                sessionStorage.removeItem("user_info_str");
                sessionStorage.removeItem("redirect_params");
			};
			this.$ajax({
				url: 'api/login/login',
				method: 'get',
				params: {},
				func: {
					success: success,
					fail: fail
				}
			});
        },

	}
};
</script>
<style lang="scss">
    main {
        min-height: calc(100vh - 131px);
    }
    @import '../../../assets/css/homepage.scss';

    .fade-transform-leave-active,
    .fade-transform-enter-active {
        transition: all .2s ease;
    }

    .fade-transform-enter {
        opacity: 0;
        transform: translateX(-30px);
    }

    .fade-transform-leave-to {
        opacity: 0;
        transform: translateX(30px);
    }    

</style>
